(function ($) {

    var defaults = {
        name: 'formVerify',
        version: '1.0',
        ele: 'body',
        errorEle: ''
    }

    $.extend({
        verifyFormZy: function (options) {

            var formItem = [
                { type: 'name', reg: /^[\u4e00-\u9fa5]{2,4}$/, error: '姓名格式为2~4字中文' },
                { type: 'idCard', reg: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/, error: '身份证输入错误' },
                { type: 'email', reg: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, error: 'Email输入错误' },
                { type: 'mobile', reg: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/, error: '手机号码输入错误' },
                { type: 'date', reg: /^\d{4}-\d{1,2}-\d{1,2}/, error: '日期格式输入错误' },
                { type: 'zip', reg: /[1-9]\d{5}(?!\d)/, error: '邮政编码格式输入错误' },
                { type: 'tel', reg: /^[0]\d{2,3}-\d{7,8}/, error: '电话号码格式输入错误' },
                { type: 'price', reg: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, error: '价格格式输入错误' },
                { type: 'notNull', reg: /\S/, error: '表单为必填项' }
            ]

            var result = true;

            var opt = $.extend(defaults, options || {})

            var errorEle = $(opt.errorEle)
            $(opt.ele + ' input').each(function () {
                verify($(this).data('type'), $(this))
                if (!result) {
                    return false
                }
            })

            if (result) {
                opt.success()
            }

            function verify(value, ele) {
                errorEle.html('')
                switch (value) {
                    case value:
                        verifyFn(value, ele)
                        break;
                }
            }

            function verifyFn(type, ele) {
                for (var i = 0; i < formItem.length; i++) {
                    if (formItem[i].type == type) {
                        if (!formItem[i].reg.test(ele.val())) {
                            errorEle.html(formItem[i].error)
                            ele.focus()
                            result = false
                        }
                    }
                }
            }
        }
    })


})(jQuery)